<template>
  <div>
    <!-- 三级分类结构 -->
    <Category :scene="scene"></Category>
    <el-card style="margin: 10px 0px">
      <!-- v-if/v-show都可以切换不同组件，推荐用v-show,因为v-if组件会销毁重建，影响性能 -->
      <!-- 场景0 -->
      <div v-show="scene == 0">
        <el-button
          type="primary"
          size="default"
          @click="addSpu"
          icon="Plus"
          :disabled="categoryStore.c3Id ? false : true"
          >添加SPU</el-button
        >
        <el-table style="margin: 10px 0px" border :data="records">
          <el-table-column
            label="序号"
            type="index"
            align="center"
            width="80px"
          ></el-table-column>
          <el-table-column label="SPU名称" prop="spuName"></el-table-column>
          <el-table-column
            label="SPU描述"
            prop="description"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column label="操作">
            <template #="{ row}">
              <el-button
                type="primary"
                size="small"
                @click="addSku(row)"
                icon="plus"
                title="添加SKU"
              ></el-button>
              <el-button
                type="primary"
                size="small"
                @click="updateSpu(row)"
                icon="edit"
                title="修改SPU"
              ></el-button>
              <el-button
                type="primary"
                size="small"
                @click="findSku(row)"
                icon="View"
                title="查看SKU"
              ></el-button>
              <el-popconfirm :title="`是否确认删除${row.spuName}吗？`" width="220px"  @confirm="deleteSpu(row)">
                <template #reference>
                  <el-button
                    type="primary"
                    size="small"
                    icon="delete"
                    title="删除SPU"
                  ></el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          v-model:current-page="pageNo"
          v-model:page-size="pageSize4"
          :page-sizes="[3, 5, 7, 9]"
          :background="true"
          layout="prev, pager, next, jumper,->, sizes,total,"
          :total="total"
          @current-change="getHasSpu"
          @size-change="changeSize"
        />
      </div>
      <!-- 添加SPU -->
      <SpuFrom v-show="scene == 1" @changeScene="changeScene" ref="spu"></SpuFrom>
      <!-- 添加SKU -->
      <SkuForm v-show="scene == 2" @changeScene="changeScene" ref="sku"></SkuForm>
      <!-- dialog对话框 -->
      <el-dialog title="SKU列表" v-model="show">
        <el-table :data="skuArr" border stripe>
          <el-table-column label="SKU名称" prop="skuName"> </el-table-column>
          <el-table-column label="SKU价格" prop="price"> </el-table-column>
          <el-table-column label="SKU重量" prop="weight"> </el-table-column>
          <el-table-column label="SKU图片">
            <template #="{ row}">
              <img :src="row.skuDefaultImg" alt="" style="width: 100px; height: 100px" />
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
    </el-card>
  </div>
</template>

<script setup lang="ts">
//引入子组件
import SpuFrom from "./spuFrom.vue";
import SkuForm from "./skuFrom.vue";
import type { HasSpuResponseData, Records, SkuData } from "@/api/product/spu/type";
import { reqHasSpu, reqRemoveSpu } from "@/api/product/spu";
//场景数值
import { ref, watch,onBeforeUnmount } from "vue";
import userCategoryStore from "@/store/modules/category";
import type { SpuData } from "@/api/product/spu/type";
import { reqSkuList } from "@/api/product/spu";
import { ElMessage } from "element-plus";
let scene = ref<number>(0); //0:显示已有SPU，1：添加或者修改已有SPU 2:添加SKU
//每一页展示几条数据
let pageSize4 = ref<number>(3);
//分页器默认页码
let pageNo = ref<number>(1);
// 存储请求数据
let records = ref<Records>([]);
//引入分类的仓库

let categoryStore = userCategoryStore();

//存储已有spu总数
let total = ref<number>(0);

//获取子组件实例
let spu = ref<any>();

//获取子组件实例
let sku = ref<any>();
//存储全部的SKU
let skuArr = ref<SkuData[]>([]);
//控制响应式数据控制隐藏
let show = ref<boolean>(false);

//监听三级分类
watch(
  () => categoryStore.c3Id,
  () => {
    if (!categoryStore.c3Id) {
      return;
    }
    getHasSpu();
  }
);
//此方法执行
const getHasSpu = async (pager = 1) => {
  //修改当前页码
  pageNo.value = pager;
  let result: HasSpuResponseData = await reqHasSpu(
    pageNo.value,
    pageSize4.value,
    categoryStore.c3Id
  );

  if (result.code == 200) {
    records.value = result.data.records;
    total.value = result.data.total;
  }
};
//分页器下拉菜单获取数据
const changeSize = () => {
  getHasSpu();
};
//点击添加新的spu按钮回调
const addSpu = () => {
  //点击切换场景一
  scene.value = 1;
  //点击添加子组件spu方法
  spu.value.initAddSpu(categoryStore.c3Id);
};
//子组件SpuForm绑定自定义事件：目前是让子组件通知父组件切换场景为0
const changeScene = (obj: any) => {
  console.log(obj.flag, obj.params);
  //子组件SpuForm点击取消变为场景0：去展示已有的SPU
  scene.value = obj.flag;
  //更新完后再次获取更新后的数据(更新留在当前页，添加留在第一页)
  if (obj.params == "updata") {
    getHasSpu(pageNo.value);
  } else {
    getHasSpu(records.value.length>1?pageNo.value:pageNo.value-1);
  }
};
//修改已有的SPU的按钮的回调
const updateSpu = (row: SpuData) => {
  scene.value = 1;
  //子组件SpuForm点击取消变为场景0：去展示已有的SPU
  //调用子组件实例方法获取完整已有的spu的数据
  spu.value.initHasSpuData(row);
};

//添加SKU回调
const addSku = (row: SpuData) => {
  //点击切换场景二
  scene.value = 2;

  sku.value.initSkuDate(categoryStore.c1Id, categoryStore.c2Id, row);
};
//查看SKU
const findSku = async (row: SpuData) => {
  let result = await reqSkuList(row.id as number);
  if (result.code == 200) {
    skuArr.value = result.data;
    show.value = true;
  }
};
//删除SPU回调
const deleteSpu = async(row:SpuData)=>{
   let result:any = await reqRemoveSpu(row.id as number)
   console.log(result)
   if(result.code == 200){
    ElMessage({
      type:'success',
      message:'删除成功'
    })
    getHasSpu()
   }else{
    ElMessage({
      type:'error',
      message:'删除失败'
    })
   }
}
//路由组件销毁前，清楚仓库关于分类的数据

onBeforeUnmount(()=>{
  categoryStore.$reset()
})
</script>

<style scoped></style>
